﻿#-------------------------------------------------------------------------------
# Copyright 2011 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#-------------------------------------------------------------------------------
#$Id: Python学习笔记（4）：控制流.py 4 2011-03-22 15:48:45Z fangqing.fan@gmail.com $
#$Author: fangqing.fan@gmail.com $
#$Date: 2011-03-22 15:48:45 +0000 (Tue, 22 Mar 2011) $
#$Rev: 4 $
#$URL: http://fanfq-python-demo.googlecode.com/svn/trunk/helloworld/Python%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%EF%BC%884%EF%BC%89%EF%BC%9A%E6%8E%A7%E5%88%B6%E6%B5%81.py $
#-------------------------------------------------------------------------------
#hello world
print("hello world")
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
length = 5
breadth = 2
area = length * breadth

print("area is",area)
print("perimeter is",2 * (length + breadth))
#-------------------------------------------------------------------------------
#Python学习笔记（4）：控制流
#Python的控制语句有if、while、for、break、continue。
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
#1. if语句
#下面是一个猜数字游戏例子：
number = 10
guess = int(input("Enter a integer:"))

if(guess == number):
    print("Congratulations, you guessed it.")
elif guess < number:
    print("No, it is a little higher than that")
else:
    print("No, it is a little lower than that")
#注意if语句结尾处包含一个冒号（：），所有控制语句结尾处都需要冒号。Python没有switch语句，您可以用if...elif...else来完成相同功能。
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
#2. while语句
#您可能发现上面猜数字游戏，每次都要启动一下才能猜，下面可以用while语句来控制直到你猜中才退出游戏。
number = 10
running = True

while running:
    guess = int(input("Enter a integer:"))

    if(guess == number):
        print("Congratulations, you guessed it.")
        running = False
    elif guess < number:
        print("No, it is a little higher than that")
    else:
        print("No, it is a little lower than that")
else:
    print("The game is over.");
#其实与while相对的else是多余的，与直接把它的语句放在在while语句块后面的效果相同。
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
#3. for语句
for i in range(1,5):
    print(i)
else:
    print("The for loop is over.");

#输出结果为：
#1
#2
#3
#4
#The for loop is over.
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
#4. break语句
while True:
    s = input("Enter something:")
    if s == "quit":
        break
    print("Length of the string is",len(s))
print("Done")
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
#5. continue语句
while True:
    s = input("Enter something:")
    if s == "quit":
        break
    if len(s) > 3:
        continue
    print("Input is of sufficient length")
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
